package com.apex.aolonfitdemo.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.apex.aolonfit.AolonFitManager;
import com.apex.aolonfitdemo.R;
import com.apex.aolonfitdemo.databinding.ActivityMusicBinding;
import com.apex.aolonfitdemo.dialog.WaitingDialog;
import com.apex.ax_bluetooth.core.EABleManager;
import com.apex.ax_bluetooth.enumeration.EABleConnectState;
import com.cyber.bridge.module.bean.MusicPlayerInfoEntity;
import com.cyber.bridge.module.bean.RaiseLightScreenEntity;
import com.cyber.bridge.module.listener.OnCyberMusicPlayListener;

public class MusicActivity extends AppCompatActivity {
    ActivityMusicBinding activityMusicBinding;
    private WaitingDialog waitingDialog;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x42) {

                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }

            } else if (msg.what == 0x43) {
                if (waitingDialog != null) {
                    if (waitingDialog.isShowing()) {
                        waitingDialog.dismiss();
                    }
                }
                Toast.makeText(MusicActivity.this, getString(R.string.modification_failed), Toast.LENGTH_SHORT).show();
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMusicBinding = ActivityMusicBinding.inflate(getLayoutInflater());
        setContentView(activityMusicBinding.getRoot());
        activityMusicBinding.tool.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(activityMusicBinding.tool);
        getSupportActionBar().setTitle(null);
        activityMusicBinding.tool.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        AolonFitManager.getInstance().setOnMusicPlayListener(new OnCyberMusicPlayListener() {
            @Override
            public void setMusicControlSwitch(boolean b) {

            }

            @Override
            public void setMusicPlayState(boolean b) {

            }

            @Override
            public void setMusicPlayerTitle(boolean b) {

            }

            @Override
            public void setMusicPlayerArtist(boolean b) {

            }

            @Override
            public void setMusicPlayerVolume(boolean b) {

            }

            @Override
            public void setMusicPlayerAllInfo(boolean b) {
                if (mHandler != null) {
                    mHandler.sendEmptyMessage(b ? 0x42 : 0x43);
                }

            }
        });
        activityMusicBinding.submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    if (waitingDialog == null) {
                        waitingDialog = new WaitingDialog(MusicActivity.this);
                    }
                    waitingDialog.show();
                    AolonFitManager.getInstance().setMusicPlayerAllInfo(new MusicPlayerInfoEntity(true, true, "千千阙歌", "陈慧娴", 60));
            }
        });
    }

    @Override
    protected void onDestroy() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
            mHandler = null;
        }
        super.onDestroy();
    }
}
